package com.lanou.student.mapper.impl;

import com.lanou.student.bean.Student;
import com.lanou.student.mapper.StudentMapper;
import com.lanou.student.util.ClassUtil;
import com.lanou.student.util.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zyj
 * @date 2022年06月01日 9:19
 */
public class StudentMapperImpl implements StudentMapper {

    private Sheet sheet;
    private Workbook workbook;
    public StudentMapperImpl(){
        this.workbook = ExcelUtil.getWorkbook("user.xlsx");
        this.sheet = ExcelUtil.getSheet(workbook, "student");

    }

    static {
        try {
            Workbook workbook = ExcelUtil.getWorkbook("user.xlsx");
            Sheet sheet = ExcelUtil.getSheet(workbook, "student");
            Row row = ExcelUtil.getRow(sheet, 0);
            ExcelUtil.setCellValue(row, "id", "stuName"
                    , "stuAge", "math", "eng", "computer");
            workbook.write(new FileOutputStream("user.xlsx"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Student> getStudents() {
        int first = sheet.getFirstRowNum() + 1;
        int last = sheet.getLastRowNum();
        List<Student> students = new ArrayList<>();
        for (int i = first; i <= last; i++) {
            Student student = ClassUtil.getObj(sheet.getRow(i),Student.class);
            students.add(student);
        }
        return students;
    }

    @Override
    public boolean add(Student s) {
        Row row = sheet.createRow(sheet.getLastRowNum() + 1);
        ExcelUtil.setCellValue(row, s.getId(), s.getStuName()
                , s.getStuAge(), s.getMath(),s.getEng()
                , s.getComputer());
        OutputStream os = null;
        try {
            os = new FileOutputStream("user.xlsx");
            workbook.write(os);
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean delete(int id) {
        try {
            sheet.removeRow(ExcelUtil.getRow(sheet,getIndex(id)));
            workbook.write(new FileOutputStream("user.xlsx"));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean update(Student stu) {
        int i = getIndex(stu.getId());

        Row row = sheet.getRow(i);
        ExcelUtil.setCellValue(row,stu.getId(),stu.getStuName(),stu.getStuAge(),
                stu.getMath(),stu.getEng(),stu.getComputer());
        try {
            workbook.write(new FileOutputStream("user.xlsx"));
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private int getIndex(int id) {
        List<Student> list = getStudents();
        for ( int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (Objects.equals(id,s.getId())){
                return i+1;
            }
        }
        return -1;
    }
}
